﻿using FreeSqlDemo.Services;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Core.Extension;
using FreeSqlDemo.Modules;
using System.IO;

namespace FreeSqlDemo.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;
        private readonly NpoiExcelExportService _exzportService;

        public WeatherForecastController(ILogger<WeatherForecastController> logger, NpoiExcelExportService exzportService)
        {
            _logger = logger;
            _exzportService = exzportService;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

        /// <summary>
        /// 数据导入
        /// </summary>
        /// <param name="file">Form表单文件信息</param>
        /// <returns></returns>
        [HttpPost,Route("DataImport")]
        public string DataImport(IFormFile file)
        {
            var operationService = new NpoiExcelOperationService();
            var result = operationService.ExcelDataBatchImport(file, out string resultMsg);
            //return Json(result ? new { code = 1, msg = resultMsg } : new { code = 0, msg = resultMsg });
            return resultMsg;
        }
        [HttpPost, Route("DataExport")]
        public string DataExport()
        {
            var result = _exzportService.ExcelDataExport(out string resultMsg, out string excelFilePath);
            return resultMsg;
            //return Json(result == true ? new { code = 1, data = excelFilePath } : new { code = 0, data = resultMsg });
        }
        [HttpGet,Route("GetUser")]
        public void GetUser()
        {
            var user = new User();
            user.GetUser();
        }

        /// <summary>
        /// 使用Byte数组导出Excel文件
        /// </summary>
        /// <returns></returns>

        [HttpGet("ByBytes")]
        public async Task<ActionResult> ByBytes()
        {
            //随机生成100条数据
            var list = GenFu.GenFu.ListOf<UserInfo>(100);
            var exporter = new ExcelExporter();
            
            var bytes = await exporter.ExportAsByteArray<UserInfo>(list);
            //使用XlsxFileResult进行导出
            return new XlsxFileResult(bytes: bytes);
        }

        /// <summary>
        /// 使用流导出Excel文件
        /// </summary>
        /// <returns></returns>
        [HttpGet("ByStream")]
        public async Task<ActionResult> ByStream()
        {
            //随机生成100条数据
            var list = GenFu.GenFu.ListOf<UserInfo>(100);
            var exporter = new ExcelExporter();
            var result = await exporter.ExportAsByteArray<UserInfo>(list);
            var fs = new MemoryStream(result);
            return new XlsxFileResult(stream: fs, fileDownloadName: "下载文件");
        }

        /// <summary>
        /// 使用泛型集合导出Excel文件
        /// </summary>
        /// <returns></returns>
        [HttpGet("ByList")]
        public ActionResult ByList()
        {
            var list = GenFu.GenFu.ListOf<UserInfo>(100);
            return new XlsxFileResult<UserInfo>(data: list);
        }
    }
}
